﻿Imports BUS
Imports DTO

Public Class MH_BangDiemLop

    Private Sub MH_BangDiemLop_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Load danh sach lop
        Dim lopBUS As New LopBUS
        Dim dsLop As New List(Of LopDTO)
        dsLop = lopBUS.LayDanhSach()
        cboLop.DataSource = dsLop
        cboLop.DisplayMember = ("TenLop")
        cboLop.ValueMember = ("MaLop")
        'Load danh sách học kỳ
        Dim hkBUS As New HocKyBUS
        Dim dt As DataTable = hkBUS.LayBang()
        cboHocKy.DataSource = dt
        cboHocKy.DisplayMember = ("TenHocKy")
        cboHocKy.ValueMember = ("MaHocKy")
    End Sub

    Private Sub btnDong_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDong.Click
        Me.Close()
    End Sub

    Private Sub btnLayBangDiem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLayBangDiem.Click
        

        Dim hsBUS As New HocSinhBUS
        Dim dsHS As New List(Of HocSinhDTO)
        dsHS = hsBUS.TimKiemTheoLop(cboLop.SelectedValue)

        dgvDanhSach.Rows.Clear()
        For i As Integer = 0 To dsHS.Count - 1

            'Tính điểm TB HK 1
            Dim strSQL As String = "SELECT ( ( ((CHITIETDIEM.Diem15P+CHITIETDIEM.Diem1T * 2 ) /3)*2+CHITIETDIEM.DiemHK) /3 ) AS TBM "
            strSQL = strSQL + " FROM DIEMMONHOC INNER JOIN CHITIETDIEM ON DIEMMONHOC.MaDiemMonHoc = CHITIETDIEM.MaDiemMonHoc "
            strSQL = strSQL + " WHERE CHITIETDIEM.MaHocSinh=" + (dsHS(i).MaHocSinh).ToString() + " AND DIEMMONHOC.MaHocKy=" + cboHocKy.SelectedValue.ToString()
            Dim tvcBUS As New ThuVienChungBUS
            Dim dt As DataTable = tvcBUS.LayBang(strSQL)
            'bảng này chứ điểm trug bình của các môn hoc sinh này đã học
            'giờ duyệt các dòng để tính ra trung bình học kỳ
            Dim tong_diem_TBM As Double = 0
            For Each row As DataRow In dt.Rows
                tong_diem_TBM = tong_diem_TBM + Double.Parse(row("TBM"))
            Next
            Dim TBHK As Double
            If dt.Rows.Count = 0 Then
                TBHK = 0
            Else
                TBHK = Math.Round(tong_diem_TBM / dt.Rows.Count, 2)
            End If


            'Add 1 dong thong tin vao GridVIew
            dgvDanhSach.Rows.Add(i + 1, dsHS(i).HoTen, TBHK)
        Next

    End Sub
End Class